package controller;

import entity.T163;
import entity.Userinfo;
import service.serviceimpl.ServiceUserImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ServletJSP",urlPatterns = "/ServletJSP")
public class ServletJSP extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
//        System.out.println("xxxxxx");
//        out.print("<script>\n" +
//                "    alert(\"xxx\");\n" +
//                "</script>");
//        request.setAttribute("bl","4");//普通变量
//        T163 t163 = new T163();
//        t163.setName("对象1");
//        request.setAttribute("dx",t163);//对象
//        List<T163> list=new ArrayList<>();
//        list.add(t163);
//        request.setAttribute("list",list);//集合
//        T163 t1631 = new T163();
//        t1631.setName("对象2");
//        Map<String,T163> map=new HashMap<>();//map
//        map.put("map1",t163);
//        map.put("map2",t1631);
//        request.setAttribute("map",map);

//        ServiceUserImpl serviceUser = new ServiceUserImpl();
//        List<Userinfo> userinfos = serviceUser.executeQuery("select * from userinfo");
//        request.setAttribute("users",userinfos);
//        request.getRequestDispatcher("/demo8/t163el.jsp").forward(request,response);
        String op = request.getParameter("op");//先拿标志
       // System.out.println("ee"+response.getCharacterEncoding());
        if (op != null) {
            ServiceUserImpl serviceUser = new ServiceUserImpl();
            if (op.equals("delete")) {//做删除
                //拿到删除id
                int id= Integer.parseInt(request.getParameter("id"));
                //执行删除操作
                int i = serviceUser.executeUpdate("delete from userinfo where id=?", id);
                if(i>0){//删除成功
                   request.setAttribute("delete","删除成功");
                    show(request,response);
                }else{//删除失败
                    request.setAttribute("delete","删除失败");
                    show(request,response);
                }
            }
        }else{
            show(request,response);
        }
        //neic 查询
        out.flush();
        out.close();
    }
    //show(request,response);
    public static void show(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        ServiceUserImpl serviceUser = new ServiceUserImpl();
        List<Userinfo> userinfos = serviceUser.executeQuery("select * from userinfo");
        request.setAttribute("users",userinfos);
        request.getRequestDispatcher("/demo8/t163el.jsp").forward(request,response);
    }//第一次。。。。
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流

        out.flush();
        out.close();
    }


}
